﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MySkin.Tools
{
    class ControlAutoSize
    {
        public struct controlRect
        {
            public int Left;
            public int Top;
            public int Width;
            public int Height;
            public Font Font;
        }
        public List<controlRect> OldControl = new List<controlRect>();
        public void GetOldControlInfo(Form BeforeChangedForm)
        {
            if (OldControl.Count == 0)
            {
                controlRect currentControl;
                currentControl.Left = BeforeChangedForm.Left;
                currentControl.Top = BeforeChangedForm.Top;
                currentControl.Width = BeforeChangedForm.Width;
                currentControl.Height = BeforeChangedForm.Height;
                currentControl.Font = BeforeChangedForm.Font;
                OldControl.Add(currentControl);
                foreach (Control item in BeforeChangedForm.Controls)
                {
                    currentControl.Left = item.Left;
                    currentControl.Top = item.Top;
                    currentControl.Width = item.Width;
                    currentControl.Height = item.Height;
                    currentControl.Font = item.Font;
                    OldControl.Add(currentControl);
                }
            }
        }
        public void ChangeControlSize(Form AfterChangedForm)
        {
            float controlWidthScale = (float)AfterChangedForm.Width / OldControl[0].Width;
            float controlHeightScale = (float)AfterChangedForm.Height / OldControl[0].Height;
            int i = 1;
            foreach (Control item in AfterChangedForm.Controls)
            {
                item.Width = (int)(OldControl[i].Width * controlWidthScale);
                item.Height = (int)(OldControl[i].Height * controlHeightScale);
                item.Left = (int)(OldControl[i].Left * controlWidthScale);
                item.Top = (int)(OldControl[i].Top * controlHeightScale);
                item.Font = new Font(OldControl[i].Font.Name, OldControl[i].Font.Size * Math.Min(controlWidthScale, controlHeightScale), OldControl[i].Font.Style, OldControl[i].Font.Unit);
                i++;
            }
            OldControl.Clear();
        }
    }
}
